package com.ruoyi.wx.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.wx.config.WxMpConfiguration;
import com.ruoyi.wx.domain.WxMpApp;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpMenuService;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;

@Component
public class WxMpUserService {

	@Autowired
	private IWxMpAppService wxMpAppService;
	
	@Autowired
	private WxMpMessageRouter wxMpMessageRouter;
	
	public String checkSignature(String requestBody,String signature,String timestamp,String nonce) {
		WxMpXmlMessage message = WxMpXmlMessage.fromXml(requestBody);
		String toUser = message.getToUser();
		WxMpApp mp = wxMpAppService.getWxMpAppByWxId(toUser);
		WxMpService wxMpService = WxMpConfiguration.getMpServices().get(mp.getAppId());
		
		if(!wxMpService.checkSignature(timestamp, nonce, signature)){
			throw new ServiceException("非法伪造请求");
		}
		WxMpXmlOutMessage outMessage;
		try {
			outMessage = wxMpMessageRouter.route(message);
		} catch (Exception e) {
			outMessage = null;
		}
		return outMessage == null ? "" : outMessage.toXml();
	}
	
	public boolean menuCreate(String appId){
		WxMpService wxMpService = WxMpConfiguration.getMpServices().get(appId);
		try {
			if(wxMpService == null){
				return false;
			}
			WxMenu menu = new WxMenu();
			WxMpMenuService menuService = wxMpService.getMenuService();
			List<WxMenuButton> buttons = new ArrayList<>();
			
			// 菜单一
			WxMenuButton menuButton = new WxMenuButton();
			menuButton.setName("选人下单");
			List<WxMenuButton> subButtons = new ArrayList<>();
			WxMenuButton subButton = new WxMenuButton();
			subButton.setName("盲盒随机单");
			subButton.setType(WxConsts.MenuButtonType.VIEW);
			subButton.setUrl("https://shudong.rbtnet.com/h5/#/pages/blind/index");
			subButtons.add(subButton);
			
			WxMenuButton subButton2 = new WxMenuButton();
			subButton2.setName("选人下单");
			subButton2.setType(WxConsts.MenuButtonType.VIEW);
			subButton2.setUrl("https://shudong.rbtnet.com/h5");
			subButtons.add(subButton2);
			
			menuButton.setSubButtons(subButtons);
			buttons.add(menuButton);
			
			// 菜单二
			WxMenuButton menuButton2 = new WxMenuButton();
			menuButton2.setName("用户中心");
			
			List<WxMenuButton> subButtons2 = new ArrayList<>();
			subButton = new WxMenuButton();
			subButton.setName("订单中心");
			subButton.setType(WxConsts.MenuButtonType.VIEW);
			subButton.setUrl("https://shudong.rbtnet.com/h5/#/pages/user/index");
			subButtons2.add(subButton);
			
			menuButton2.setSubButtons(subButtons2);
			buttons.add(menuButton2);
			
			menu.setButtons(buttons);
			menuService.menuDelete();
			menuService.menuCreate(menu);
		} catch (WxErrorException e) {
			e.printStackTrace();
		}
		return true;
	}
}
